package com.tql.mall.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tql.mall.common.result.R;
import com.tql.mall.model.product.SpuImage;
import com.tql.mall.model.product.SpuInfo;
import com.tql.mall.model.product.SpuSaleAttr;
import com.tql.mall.product.service.SpuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: tyk
 * @Date: 2023/5/22 12:25
 * @Description:
 */

@RestController
@RequestMapping("admin/product/spu")
@Api(description = "Spu管理")
public class SpuController {

    @Autowired
    private SpuService spuService;

    /**
     * 根据三级分类ID查询spu列表
     */
    @GetMapping("{page}/{limit}")
    @ApiOperation("根据三级分类ID查询spu列表")
    public R<IPage<SpuInfo>> getSpuInfoList(@PathVariable Long page,
                                            @PathVariable Long limit,
                                            Long category3Id,
                                            Long shopId) {
        // 封装参数
        Page<SpuInfo> pageInfo = new Page<>(page, limit);
        // 查询
        IPage<SpuInfo> res = spuService.getSpuInfoList(category3Id, shopId, pageInfo);
        // 返回结果
        return R.ok(res);
    }

    /**
     * 保存Spu
     */
    @PostMapping("saveSpu")
    @ApiOperation("保存Spu")
    public R<Void> saveSpuInfo(@RequestBody SpuInfo spuInfo) {
        spuService.saveSpu(spuInfo);
        return R.ok();
    }

    /**
     * 根据spuID查询销售属性和销售属性值集合
     */
    @GetMapping("saleAttrList/{spuId}")
    @ApiOperation("根据spuID查询销售属性和销售属性值集合")
    public R<List<SpuSaleAttr>> saleAttrList(@PathVariable Long spuId) {
        List<SpuSaleAttr> list = spuService.saleAttrList(spuId);
        return R.ok(list);
    }

    /**
     * 根据SpuID查询图片列表
     */
    @GetMapping("imageList/{spuId}")
    @ApiOperation("根据SpuID查询图片列表")
    public R<List<SpuImage>> imageList(@PathVariable Long spuId) {

        List<SpuImage> imageList = spuService.imageList(spuId);
        return R.ok(imageList);
    }

}
